import {
  ByteUtils
} from './ByteUtils';
let byteUtils = new ByteUtils();
import {
  ManufacturerData
} from '../entity/ManufacturerData';

const EIGHT_HOURS = 8 * 60 * 60 * 1000;
class ProtocolUtils {


  /**
   * 根据用户ID数组获取整形用户ID
   * @param {用户数组} userIdArray 
   */
  getUserId(userIdArray) {
    let userId = (userIdArray[0] << 24) & 0xff000000;
    userId |= (userIdArray[1] << 16) & 0xff0000;
    userId |= (userIdArray[2] << 8) & 0xff00;
    userId |= (userIdArray[3]) & 0xff;
    console.log("userId = " + userId);
    return userId;
  }

  /**
   * 根据用户ID获取数组
   * @param {用户Id} userId 
   */
  getUserIdArray(userId) {
    let userIdArray = new Array();
    userIdArray.push((userId & 0xff000000) >> 24);
    userIdArray.push((userId & 0xff0000) >> 16);
    userIdArray.push((userId & 0xff00) >> 8);
    userIdArray.push((userId & 0xff));
    console.log("userIdArray = " + userIdArray);
    return userIdArray;
  }

  /**
   * 根据时间字符串获取时间
   * @param {int组成的时间字符串} dateArray 
   */
  getTimeByArray(dateArray) {
    console.log(dateArray)
    let fullYear = dateArray[0] + 2000;
    let month = dateArray[1] - 1;
    let day = dateArray[2];
    let hours = dateArray[3];
    let minutes = dateArray[4];
    let seconds = dateArray[5];
    let date = new Date(Date.UTC(fullYear, month, day, hours, minutes, seconds) - EIGHT_HOURS);
    console.log(date)
    return date;
  }

  /**
   * 获取时间字符串
   * @param {时间} date 
   * 不传参数则获取当前时间的字符串
   */
  getTimeArray(date) {
    let curDate = new Date();
    if (date instanceof Date) {
      curDate = date;
    }
    let fullYear = curDate.getFullYear();
    fullYear = fullYear % 100;
    let month = curDate.getMonth() + 1;
    let day = curDate.getDate();
    let hours = curDate.getHours();
    let minutes = curDate.getMinutes();
    let seconds = curDate.getSeconds();
    // console.log("时间：" + fullYear + "年" + month + "月" + day + "日" + hours + "时" + minutes + "分" + seconds + "秒")
    let dateArray = new Array();
    dateArray.push(fullYear)
    dateArray.push(month)
    dateArray.push(day)
    dateArray.push(hours)
    dateArray.push(minutes)
    dateArray.push(seconds)
    return dateArray
  }
  /**
   * 获取蓝牙Mac地址字符串
   * @param {蓝牙mac地址的数组} mac 
   */
  getBleMacStr(mac) {
    let macString = byteUtils.ab2hex(mac, ":");
    console.log("macString = " + macString.toUpperCase())
    return macString.toUpperCase();
  }

  /**
   * 解析电量
   * 返回四位数整形
   */
  getPower(powerH, powerL) {
    let power = ((powerH & 0xff) << 8) + (powerL & 0xff);
    power = power / 100;
    power = power.toFixed(2);
    console.log(" power = " + power);
    return power;
  }

  /**
   * 解析信号
   * 返回四位数整形
   */
  getSignal(signalH, signalL) {
    let signal = signalH & 0xff << 8 + signalL & 0xff;
    // console.log("signal = " + signal)
    return signal;
  }

  /**
   * 
   * @param {版本字符串，例：1.2.2.1} version 
   */
  getVersionBytes(version) {
    let result = new Array();
    let vArray = version.split(".");
    let vH = (vArray[0] << 4 & 0xf0) | (vArray[1] & 0xf)
    let vL = (vArray[2] << 4 & 0xf0) | (vArray[3] & 0xf)
    result.push(vH)
    result.push(vL)
    return result;
  }

  /**
   * 解析版本
   * 放回版本字符串
   */
  getVersion(versionH, versionL) {
    let v1 = (versionH >> 4) & 0xf;
    let v2 = versionH & 0x0f;
    let v3 = (versionL >> 4) & 0xf;
    let v4 = versionL & 0x0f;
    // console.log("v1 = " + v1 + " v2 = " + v2 + " v3 = " + v3 + " v4 = " + v4);
    let version = "" + v1 + "." + v2 + "." + v3 + "." + v4;
    // console.log(version)
    return version;
  }

  /**
   * 解析广播ManufacturerData
   */
  /**
   * 解析Manufactured Data 数据
   * @param {广播数据} tempadvertisData 
   */
  parseManufacturerData(tempadvertisData) {
    console.log("--------解析广播数据--------")
    if (tempadvertisData.byteLength == 0) {
      console.log("该蓝牙设备没有广播单位数据")
      return null;
    }
    // let advertisData = byteUtils.ab2Array(tempadvertisData);
    // console.log(byteUtils.ab2hex(advertisData));
    // let manufacturerData = new ManufacturerData();
    // manufacturerData.companyCode = ((advertisData[0] & 0xff) << 8) + (advertisData[1] & 0xff);
    // manufacturerData.typeCode = byteUtils.bytes2ToInt(advertisData.slice(2, 4));
    // manufacturerData.supportFunctionBase = advertisData[4];
    // manufacturerData.supportFunctionOther = advertisData[5];
    // manufacturerData.softwareVersion = this.getVersion(advertisData[6], advertisData[7]);
    // manufacturerData.power = this.getPower(advertisData[8], advertisData[9]);
    // // manufacturerData.power = (Math.round(tempPower)) / 100;
    // manufacturerData.bleMacAddress = advertisData.slice(10, 16);
    // manufacturerData.isRegister = advertisData[16] == 1 ? true : false;
    // let pVersion = Number(((advertisData[17] & 0xf0) >> 4).toString(16) + "." + (advertisData[17] & 0x0f).toString(16));
    // manufacturerData.protocolVersion = pVersion;
    // manufacturerData.broadcastReason = advertisData[18];
    // console.log(manufacturerData)
    return manufacturerData;
  }

  /**
   * 根据mac地址数组获取mac地址String
   * @param {macAddress数组} macBytes 
   */
  getMacStrByMacBytes(macBytes, version) {
    let macStr = "";
    if (version < 3.2) {
      for (let i = macBytes.length - 1; i >= 0; --i) {
        macStr += ('00' + macBytes[i].toString(16)).slice(-2);
        if (i != 0) {
          macStr += ":";
        }
      }
    } else {
      for (let i = 0; i < macBytes.length; ++i) {
        macStr += ('00' + macBytes[i].toString(16)).slice(-2);
        if (i != (macBytes.length - 1)) {
          macStr += ":";
        }
      }
    }
    return macStr.toUpperCase();
  }

  /**
   * 广播数据
   * @param {蓝牙广播中的advertisData} tempadvertisData 
   */
  getMacStrByManufurturerData(tempadvertisData) {
    if (tempadvertisData.byteLength == 0) {
      console.log("该蓝牙设备没有广播单位数据")
      return null;
    }
    let advertisData = byteUtils.ab2Array(tempadvertisData);
    let bleMacAddress = advertisData.slice(10, 16);
    let pVersion = ((advertisData[17] & 0xf0) >> 4).toString(16) + "." + (advertisData[17] & 0x0f).toString(16);
    let nVersion = Number(pVersion);
    console.log("protocol version = " + nVersion);
    return this.getMacStrByMacBytes(bleMacAddress, nVersion);
  }

  /**
   * 根据广播数据判断是否需要连接
   * @param {广播数据} tempadvertisData 
   */
  getBroadcastReason(tempadvertisData) {
    console.log("--------解析广播数据--------")
    if (tempadvertisData.byteLength == 0) {
      console.log("该蓝牙设备没有广播单位数据")
      return -1;
    }
    let advertisData = byteUtils.ab2Array(tempadvertisData);
    return advertisData[18];
  }
}

export {
  ProtocolUtils
}